package com.minio.miniospringboot;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;


/**
 * @ClassName: FileUploader
 * @Desc: 上传文件
 * @auther: <Bang>
 * @Date: 2023/03/17 14:31
 * @Version: 1.0
 * @Modified By:
 */
public class FileUploader {

    private static final String ENDPOINT = "http://192.168.96.156:9000";
    private static final String ACCESSKEY = "adminMinio";
    private static final String SECRETKEY = "adminMinio";
    private static final String BUCKET = "files";
    ;
    public static final String OBJECT_NAME = "test.zip";
    public static final String FILENAME = "F:\\test.zip";

    public static void main(String[] args)
            throws Exception {
        try {
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(ENDPOINT)
                    .credentials(ACCESSKEY, SECRETKEY).build();
            // 创建bucket
            boolean exists =
                    minioClient.bucketExists(
                            BucketExistsArgs.builder()
                                    .bucket(BUCKET).build()
                    );
            if (!exists) {
                // 不存在，创建bucket
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET).build());
            }
            // 上传文件
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(BUCKET)
                            .object(OBJECT_NAME)
                            .filename(FILENAME)
                            .build());
            System.out.println("上传文件成功");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }
}
